package com.mg.plugincqamg.calculator;

import com.mg.model.Model;
import com.mg.plugincqamg.entity.StationData;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class CloudyAndRainyCalculator {
    public static float calcCRLevel(List<StationData> stationDataList, Model model) throws ParseException {
        //数据排序
        List<StationData> sortStationDataList = sortStationDataByDate(stationDataList, model);
        int level = 0;
        int tmp = 0;
        List<StationData> calcStationDataList = new ArrayList<StationData>();
        for (StationData stationData : sortStationDataList) {
            if (stationData.getSsh() <= 3.0) {
                calcStationDataList.add(stationData);
            } else {
                tmp = calcCRLevel(calcStationDataList);
                if (tmp > level) {
                    level = tmp;
                }
                calcStationDataList.clear();
            }
        }
        return level;
    }

    private static int calcCRLevel(List<StationData> calcStationDataList) {
        int day = calcCRSuitDay(calcStationDataList);
        if (day > 14) {
            return 7;
        } else if (day > 6) {
            return 5;
        } else if (day >= 3) {
            return 3;
        } else {
            return -1;
        }
    }

    private static int calcCRSuitDay(List<StationData> calcStationDataList) {
        double totalRain = 0;
        int count;
        int maxDay = 0;
        for (int i = 0; i < calcStationDataList.size(); i++) {
            count = 0;
            for (int j = i; j < calcStationDataList.size(); j++) {
                totalRain += calcStationDataList.get(j).getEr24();
                count++;
                if (totalRain / count >= 4.0) {
                    if (count > maxDay) {
                        maxDay = count;
                    }
                }
            }
        }
        return maxDay;
    }

    private static List<StationData> sortStationDataByDate(List<StationData> stationDataList, Model model) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        StationData[] stationDatas = new StationData[stationDataList.size()];
        stationDatas = stationDataList.toArray(stationDatas);
        StationData tmp;
        for (int i = 1; i < stationDatas.length; i++) {
            for (int j = 0; j < i; j++) {
                if (sdf.parse(stationDatas[i].getDate()).before(sdf.parse(stationDatas[j].getDate()))) {
                    tmp = stationDatas[i];
                    stationDatas[i] = stationDatas[j];
                    stationDatas[j] = tmp;
                }
            }
        }
        List<StationData> resultStationDataList = new ArrayList<StationData>();
        for (StationData stationData : stationDatas) {
            resultStationDataList.add(stationData);
//            model.OutputLog(Model.LogLevel.Info, stationData.getStationId() + ":" + stationData.getDate() + "ER24===>" + stationData.getEr24());
        }
        return resultStationDataList;
    }
}
